from Object3D import *
from OpenGL.GL import *
from OpenGL.GLU import *
from math import *
from Vector import *

class Ball(Object3D):

	def __init__(self, parent=None):
		super(Ball, self).__init__()
		self.parent = parent
		self.radius = 0.02 # official size = 40 mm diameter
		self.gravity = 9.8
		
	def draw(self):

		# ball
		glPushMatrix()
		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.9,0.9,0.9,1.0])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.9,0.9,0.9,1.0])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.6,0.6,0.6,1.0])

		glTranslatef(*self.position.list())

		sphere = gluNewQuadric()
		gluQuadricOrientation(sphere, GLU_OUTSIDE)
		gluSphere(sphere, self.radius, 20, 15)
		glPopMatrix()

		# shadow
		glPushMatrix()
		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.15,0.15,0.15,0.7])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.15,0.15,0.15,0.7])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.0,0.0,0.0,1.0])
		glTranslatef(self.position.x, 0.02, self.position.z)
		size = 1.0 + self.position.y
		if ((self.position.z > self.parent.table.depth/2.0) or
		(self.position.z < -self.parent.table.depth/2.0) or
		(self.position.x > self.parent.table.width/2.0) or
		(self.position.x < -self.parent.table.width/2.0)):
			glTranslatef(0,-self.parent.table.height,0)
			size += self.parent.table.height
		glRotate(90,1,0,0)
		disk = gluNewQuadric()
		gluQuadricOrientation(disk, GLU_INSIDE)
		gluDisk(disk, 0, self.radius*size, 20, 1)
		glPopMatrix()


	def animate(self, interval, gravityIsEnabled):

		if (gravityIsEnabled):
			# v = v0 - g*t
			self.velocity.y -= self.gravity*interval

		# s = s0 + vt
		self.position += self.velocity*interval
